import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hx/base/loading_dialog.dart';
import 'package:flutter_hx/base/utils/toast_utils.dart';
import 'package:flutter_hx/network/HttpGo.dart';
import 'package:flutter_hx/services/user_service.dart';
import 'package:get/get.dart';

import '../root/pm/pm_logic.dart';

class LoginLogic extends GetxController {
  var _pageIndex=0;
  int get  pageIndex=>_pageIndex;
  set pageIndex(int pageIndex){
    _pageIndex=pageIndex;
    _loginPhone="";
    _loginPassword="";
    _rgsPhone="";
    _rgsPassword="";
    _rgsSmsCode="";
    _rgsShareCode="";
    isRgsButtonClick=false;
    isLoginButtonClick=false;
    update();
  }
  var isLoginButtonClick=false;
  var isRgsButtonClick=false;
  var _loginPhone="";
  var _loginPassword="";

  var _rgsPhone="";
  var _rgsPassword="";
  var _rgsSmsCode="";
  var _rgsShareCode="";



  set rgsPhone(String rgsPhone){
    _rgsPhone=rgsPhone;
    updateRgsButtonClick();
  }
  set rgsPassword(String rgsPassword){
    _rgsPassword=rgsPassword;
    updateRgsButtonClick();
  }
  set rgsSmsCode(String rgsSmsCode){
    _rgsSmsCode=rgsSmsCode;
    updateRgsButtonClick();
  }
  set rgsShareCode(String rgsShareCode){
    _rgsShareCode=rgsShareCode;
    updateRgsButtonClick();
  }

  set loginPhone(String loginPhone){
    _loginPhone=loginPhone;
    final news=_loginPhone.isNotEmpty&&_loginPassword.isNotEmpty;
    final isUpdate=news!=isLoginButtonClick;
    if(isUpdate){
      isLoginButtonClick=news;
      update();
    }
  }

  set loginPassword(String loginPassword){
    _loginPassword=loginPassword;
    final news=_loginPhone.isNotEmpty&&_loginPassword.isNotEmpty;
    final isUpdate=news!=isLoginButtonClick;
    if(isUpdate){
      isLoginButtonClick=news;
      update();
    }
  }

 void updateRgsButtonClick(){
   final news=checkRgsButtonIsClick();
   final isUpdate=news!=isLoginButtonClick;
   if(isUpdate){
     isRgsButtonClick=news;
     update();
   }
 }


  bool checkRgsButtonIsClick()=>_rgsPhone.isNotEmpty&&_rgsSmsCode.isNotEmpty&&_rgsPassword.isNotEmpty&&_rgsShareCode.isNotEmpty;

  Future doLogin()async {
    FocusScope.of(Get.context!).unfocus();
    LoadingDialog.show(Get.context!);
    final params={
      "client_id":"app",
      "client_secret":"app",
      "grant_type":"app",
      "scope":"server",
      "phone":_loginPhone,
      "password":_loginPassword
    };
   final res= await HttpGo.instance.get("/auth/oauth/token",params:params).whenComplete(() => Get.back());
   res.ifSuccess((data){
     UserService.to.login(data);
     Get.back();
     var logic = Get.find<PmLogic>();
     logic.getList();
   });
   res.ifFailure((e) => e.toast());
  }

  ///注册
  Future doRgs()async{
    LoadingDialog.show(Get.context!);
    final params={
        "area": "",
        "city": "",
        "password": _rgsPassword,
        "phone": _rgsPhone,
        "province": "",
        "shareCode": _rgsShareCode,
        "smsCode": _rgsSmsCode
    };
    final res=await HttpGo.instance.post("/member/register",data: params).whenComplete(() => Get.back());
    res.ifFailure((e) => e.toast());
    res.ifSuccess((data) {
      ToastUtils.show("注册成功");
      pageIndex=0;
    });
  }

}


